package point

import (
	"math/rand"
	"strings"
)

// ParseFieldsTemplate 解析 fields 模板字符串
func ParseFieldsTemplate(template string) map[string]string {
	fieldsMap := make(map[string]string)
	if strings.HasPrefix(template, "fields=") {
		template = strings.TrimPrefix(template, "fields=")
	}
	parts := strings.Split(template, ",")
	for _, part := range parts {
		sub := strings.Split(part, ":")
		if len(sub) == 2 {
			fieldsMap[sub[0]] = sub[1] // key=字段名, value=类型
		}
	}
	return fieldsMap
}

// GenerateFieldValue 根据类型生成随机值
func GenerateFieldValue(fieldType string) interface{} {
	switch fieldType {
	case "int":
		return rand.Intn(100)
	case "float":
		return rand.Float64() * 100
	case "bool":
		return rand.Intn(2) == 1
	default:
		return nil
	}
}
